/**********************************************************************
gc_13_structural_counterfactuals_save.do

**********************************************************************/
**********
* SET UP *
**********
clear all
set matsize 2000
set more 1

* location for dofiles *
cd "T:\_Projet_4915\dofiles"
global dir "`c(pwd)'"
cd $dir

set seed 8397740

sysdir set PERSONAL "T:\_Projet_4915\ado"

* specify file locations *
global project_folder "\_Projet_4915"
global data_folder "\_Projet_4915\DATA"
global output_folder "\_Projet_4915\ResultsFolder"
global temp "temp"

local datadir T:\${data_folder}\

*********
* GATES *
*********
* Specify which data you want to work with (synthetic = syn, real = rl) *
local ext = "rl"

* Specify which gender to process (men = men) *
local gender = "men"

* Specify first observed age (24 25 26) *
local a = 25

* Specify occupational definition to use (occ_version 1 2)
local v = 2

* Specify firm class definition (1 2 3 4 5 6)
local d = 6

* Specify the number of unobservable types *
global types  "5"

* Specify earnings data generating process *
local y_dgp = 3

* save results, full model *
local gate7 = 1

* STEP X: delete all intermediate datasets *
local gateX = 1

*******************************************
* MAKE A GLOBAL VARIABLE FOR TODAY'S DATE *
*******************************************
local tyr = substr("$S_DATE",8,4)
local tmo = substr("$S_DATE",4,3)
local tmd = trim(substr("$S_DATE",1,2))

* make day of month two digits *
local wl = length("`tmd'")
if `wl'==1 {
local tmd2 ="0"+"`tmd'"
}
if `wl'!=1 {
local tmd2 ="`tmd'"
}

* get numeric month, make it two digits *
local di="`tmd2'"+"`tmo'"+"`tyr'"
local edate = date("`di'", "DMY")
local mono = month(`edate')
local ml = length("`mono'")
if `ml'==1 {
local mono2 ="0"+"`mono'"
}
if `ml'!=1 {
local mono2 ="`mono'"
}

* put final date together *
global date = "`tyr'"+"`mono2'"+"`tmd2'"

****************************************
* DEFINE LITTLE PROGRAMS TO PRINT TIME *
****************************************
program define starttime
	display "Started processing at $S_TIME on $S_DATE"
end

program define endtime
	display "Finished processing at $S_TIME on $S_DATE"
end


************************
* START OF THE PROGRAM *
************************
foreach n_types of global types ///
{
* start log file *
quietly capture log close
quietly log using gc_13_counterfactuals_save_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'_`ext', text replace

********************
* Save the results *
********************
disp "***** Started processing STEP 7 *****"
starttime
if 1 == `gate7' ///
{

foreach sim in "fit" "C8" "C9" "C5" "C6" ///
{
	local datadir T:\${data_folder}\
	use "`datadir'gc_structural_`sim'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", clear
	sort pid_int t
	gen q = .
	forvalues s = 1(1)`n_types' ///
	{
	    replace q = q`s' if state == `s'
	}
	tab action, gen(action_shr)

	*** calculate the mean of certain variables by type ***
	forvalues s = 1(1)`n_types' ///
	{
		preserve
		keep if state == `s'
		collapse (mean) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6 [aw=q], by(age)
		gen state = "`s'"
		gen stat_type = "mean"
  		* save it or stack it *
  		if "`s'" == "1" ///
    	{
    		save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
    	}
  		if "`s'" != "1" ///
    	{
    		append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    		save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
    	}
		restore
	}
	
	*** calculate the mean of certain variables pooling all types ***
	preserve
	collapse (mean) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6 [aw=q], by(age)
	gen state = "0"
	gen stat_type = "mean"
    append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
	restore
	
	*** calculate the mean of certain variables pooling all types (excluding type 1) ***
	preserve
	drop if state == 1
	collapse (mean) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6 [aw=q], by(age)
	gen state = "99"
	gen stat_type = "mean"
    append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
	restore
	
	*** bring means and sds and reformat ***
	preserve
	use "`datadir'gc_reduced_tmp2_`ext'.dta", clear
	keep if stat_type == "mean"
	drop stat_type
	order state age
	sort state age
	keep if age == 5 | age == 10
	save "`datadir'gc_reduced_tmp3_`ext'.dta", replace
	order state age ///
	action_shr1 ///
	action_shr2 ///
	action_shr3 ///
	action_shr4 ///
	action_shr5 ///
	action_shr6
	save "`datadir'gc_reduced_tmp4_`ext'.dta", replace
	
	* set up save table options *
	local datadir T:\${output_folder}\
	disp "`datadir'"
	export excel using "`datadir'gc_cntrfact_shr_`sim'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.xlsx", firstrow(variables) replace
	clear
	restore

	* delete intermediate datasets *
	local datadir T:\${data_folder}\
	capture erase "`datadir'gc_reduced_tmp2_`ext'.dta"
	capture erase "`datadir'gc_reduced_tmp3_`ext'.dta"
	capture erase "`datadir'gc_reduced_tmp4_`ext'.dta"
	
	* delete intermediate datasets *
	local datadir T:\${data_folder}\
	capture erase "`datadir'gc_reduced_tmp2_`ext'.dta"
	capture erase "`datadir'gc_reduced_tmp3_`ext'.dta"
	capture erase "`datadir'gc_reduced_tmp4_`ext'.dta"
	
	clear
}

}
disp "***** Finished processing STEP 7 *****"
endtime

********************************************
* STEP X: delete all intermediate datasets *
********************************************
disp "***** Started processing STEP X *****"
starttime
if 1 == `gateX' ///
{
	disp "***** STEP X: delete all intermediate datasets *****"
	local datadir T:\${data_folder}\
	
	disp "***** STEP X: delete all intermediate datasets (COMPLETED) *****"
}
disp "***** Finished processing STEP X *****"
endtime

********
* EXIT *
********
clear all
log close
